This application demonstrates how to quickly and efficiently capture the main device's desktop into a window. The program basically reads the image stored in the the main device's pixmap then copies it to a custom pixmap. The custom pixmap is defined at the same depth of the main device and contains an identical copy of that device's color-table. This is done to provide the fastest performance possible when copying from an offscreen to onscreen pixmap. By making sure the pixel values map to the exact same colors in both colortables, copybits will do a direct transfer of bits without wasting time remapping the colors. Also the ctSeed field for each colortable should be the same. Finally, since the main device's bounding rect is different than that of the offscreen's, the copying performance for the device to the offscreen is slightly affected because of the scaling required. However, the copying performance for the offscreen to the window is the best possible since the bounding rects for each are identical.
(Updated Description)
While "carbonizing" this particular sample, I decided to add a bunch of "bells and whistles" to the sample which I had seen in other samples. In addition, I decided to do a near "full" carbonization of the sample. I've added some "conditional" logic so that the menus will appear correctly for OS 9 and X (no File->Quit menu under X). I also added support for the "Quit" AppleEvent, so that under OS X selecting "Application Menu"->Quit would exit the program. In addition, dynamic resizing of windows, setting the size of the windows, saving the screenshot to a pict file, refreshing the snapshot, as well as multiple windows were all added to the program. Lastly, on OS 9, a menu items will set the desktop picture using Apple Events. While on OS X, a full screen mode display of a snapshot will be displayed (since the method to set the desktop picture on OS X was not available at the time of this writing).
Building the Code
This project was built and tested on CodeWarrior 2.1 using CarbonLib 1.1a3. Since this is a Carbon app, the "Carbon Support" folder of the CarbonSDK should be in the same folder as the CodeWarrior IDE. If this is not the case, the CarbonLib library will have to be re-added to the project. In addition, the CarbonLib extension needs to be in the Extensions folder. The CarbonSDK (which contains the library and the extension) can be found on Apple's website: http://developer.apple.com/sdk/
Restrictions
Nearly every sample requires more memory than the 384KB CodeWarrior sets as the default heap size. If for some reason the memory has been reset to the default (either through converting the project or whatever) the memory should be reset to 1000 - 2000KB (depending on the application). This can be achieved by going to the projects settings (Edit Menu->C-PPC Settings... under CodeWarrior 2.1) and selecting the "PPC Target" under the "Target" tab. This sample would be best run with 5000+ KB of memory.
Other Code Notes
Unlike other "carbonized" samples, this one has had its non-Carbon API code removed.
Known Bugs
Creating too many instances of a snapshot window (too many windows) will cause the program to overwrite memory that does not belong to it. This takes numerous windows, around 15+, in order for this to occur. Pay close attention to the memory settings for this program.